﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PatientsManagement.Database;
using System.Configuration;
using PatientsManagement.Users;
using PatientsManagement.Patients;
using PatientsManagement.Patients_and_Doctors;

namespace PatientsManagement
{
    public partial class ManagePatients : Form
    {
        bool userIsAdmin = true;
        DataTable dataTable = new DataTable();
        string username = "";

        public ManagePatients(bool userIsAdmin,string username)
        {
            InitializeComponent();
            SetVisible(true);
            this.userIsAdmin = userIsAdmin;
            this.username = username;
            user.Text = UsersData.GetUserForenameSurname(username);
            FillGridWithPatientsData();
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Escape)
                this.Dispose();
        }

        private void InitializeData()
        {
            dataTable = new DataTable();
            dataTable.DefaultView.RowFilter = "";
            dataTable.Rows.Clear();
            dataTable.Columns.Clear();
            dataTable.Columns.Add("ID", typeof(string));
            dataTable.Columns.Add("Imię", typeof(string));
            dataTable.Columns.Add("Nazwisko", typeof(string));
            dataTable.Columns.Add("Pesel", typeof(string));
            columnsCombo.Items.Clear();
        }

        private void FillGridWithPatientsData()
        {
            dataView.DataSource = PatientsData.GetPatientsTable();
            int count = dataView.ColumnCount;
            columnsCombo.Items.Clear();
            dataTable = new DataTable();
            for (int i = 0; i < dataView.ColumnCount; i++)
            {
                columnsCombo.Items.Add(dataView.Columns[i].Name);
                dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                dataTable.Columns[i].ReadOnly = true;
                dataView.Columns[i].ReadOnly = true;
            }
            for (int i = 0; i < dataView.Rows.Count; i++)
            {
                dataTable.Rows.Add(dataView.Rows[i].Cells["ID"].Value.ToString(),
                    dataView.Rows[i].Cells["Imię"].Value.ToString(),
                    dataView.Rows[i].Cells["Nazwisko"].Value.ToString(),
                    dataView.Rows[i].Cells["Miasto"].Value.ToString(),
                    dataView.Rows[i].Cells["Ulica"].Value.ToString(),
                    dataView.Rows[i].Cells["NrDomu"].Value.ToString(),
                    dataView.Rows[i].Cells["Kod_Pocztowy"].Value.ToString(),
                    dataView.Rows[i].Cells["Pesel"].Value.ToString());
            }
            columnsCombo.SelectedIndex = 0;
            filterValue.Text = "";
        }

        private void FillGridWithDoctorsData()
        {
            dataView.DataSource = DoctorsData.GetDoctorsTable();
            int count = dataView.ColumnCount;
            columnsCombo.Items.Clear();
            dataTable = new DataTable();
            for (int i = 0; i < dataView.ColumnCount; i++)
            {
                columnsCombo.Items.Add(dataView.Columns[i].Name);
                dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                dataTable.Columns[i].ReadOnly = true;
                dataView.Columns[i].ReadOnly = true;
            }
            for (int i = 0; i < dataView.Rows.Count; i++)
            {
                dataTable.Rows.Add(dataView.Rows[i].Cells["ID"].Value.ToString(),
                    dataView.Rows[i].Cells["Imię"].Value.ToString(),
                    dataView.Rows[i].Cells["Nazwisko"].Value.ToString(),
                    dataView.Rows[i].Cells["Miasto"].Value.ToString(),
                    dataView.Rows[i].Cells["Ulica"].Value.ToString(),
                    dataView.Rows[i].Cells["NrDomu"].Value.ToString(),
                    dataView.Rows[i].Cells["Kod_Pocztowy"].Value.ToString(),
                    dataView.Rows[i].Cells["Pesel"].Value.ToString(),
                    dataView.Rows[i].Cells["Specjalizacja"].Value.ToString());
            }
            columnsCombo.SelectedIndex = 0;
            filterValue.Text = "";
        }

        private void FillGridWithVisitsData()
        {
            dataView.Columns.Clear();
            if (patientsButton.BackColor == Color.Silver)
            {
                dataView.DataSource = PatientsData.GetPatientVisitsTable();
                int count = dataView.ColumnCount;
                columnsCombo.Items.Clear();
                dataTable = new DataTable();
                for (int i = 0; i < dataView.ColumnCount; i++)
                {
                    columnsCombo.Items.Add(dataView.Columns[i].Name);
                    dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                    dataTable.Columns[i].ReadOnly = true;
                    dataView.Columns[i].ReadOnly = true;
                }
                for (int i = 0; i < dataView.Rows.Count; i++)
                {
                    dataTable.Rows.Add(dataView.Rows[i].Cells["ID_Wizyty"].Value.ToString(),
                        dataView.Rows[i].Cells["Pacjent"].Value.ToString(),
                        dataView.Rows[i].Cells["Lekarz"].Value.ToString(),
                        dataView.Rows[i].Cells["Data_Wizyty"].Value.ToString());
                }
                columnsCombo.SelectedIndex = 0;
                filterValue.Text = "";
            }
            if (doctorsButton.BackColor == Color.Silver)
            {
                dataView.DataSource = DoctorsData.GetDoctorVisitsTable();
                int count = dataView.ColumnCount;
                columnsCombo.Items.Clear();
                dataTable = new DataTable();
                for (int i = 0; i < dataView.ColumnCount; i++)
                {
                    columnsCombo.Items.Add(dataView.Columns[i].Name);
                    dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                    dataTable.Columns[i].ReadOnly = true;
                    dataView.Columns[i].ReadOnly = true;
                }
                for (int i = 0; i < dataView.Rows.Count; i++)
                {
                    dataTable.Rows.Add(dataView.Rows[i].Cells["ID_Wizyty"].Value.ToString(),
                        dataView.Rows[i].Cells["Lekarz"].Value.ToString(),
                        dataView.Rows[i].Cells["Pacjent"].Value.ToString(),
                        dataView.Rows[i].Cells["Data_Wizyty"].Value.ToString());
                }
                columnsCombo.SelectedIndex = 0;
                filterValue.Text = "";
            }
        }

        private void FillGridWithPrescriptionsData()
        {
            if (patientsButton.BackColor == Color.Silver)
            {
                dataView.Columns.Clear();
                dataView.DataSource = PatientsData.GetPrescriptionsTable();
                int count = dataView.ColumnCount;
                columnsCombo.Items.Clear();
                dataTable = new DataTable();
                for (int i = 0; i < dataView.ColumnCount; i++)
                {
                    columnsCombo.Items.Add(dataView.Columns[i].Name);
                    dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                    dataTable.Columns[i].ReadOnly = true;
                    dataView.Columns[i].ReadOnly = true;
                }
                for (int i = 0; i < dataView.Rows.Count; i++)
                {
                    dataTable.Rows.Add(dataView.Rows[i].Cells["ID_Recepty"].Value.ToString(),
                        dataView.Rows[i].Cells["ID_Wizyty"].Value.ToString(),
                        dataView.Rows[i].Cells["Pacjent"].Value.ToString(),
                        dataView.Rows[i].Cells["Lekarz"].Value.ToString(),
                        dataView.Rows[i].Cells["Data_Wizyty"].Value.ToString());
                }
                columnsCombo.SelectedIndex = 0;
                filterValue.Text = "";
            }
            if (doctorsButton.BackColor == Color.Silver)
            {
                dataView.Columns.Clear();
                dataView.DataSource = DoctorsData.GetPrescriptionsTable();
                int count = dataView.ColumnCount;
                columnsCombo.Items.Clear();
                dataTable = new DataTable();
                for (int i = 0; i < dataView.ColumnCount; i++)
                {
                    columnsCombo.Items.Add(dataView.Columns[i].Name);
                    dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                    dataTable.Columns[i].ReadOnly = true;
                    dataView.Columns[i].ReadOnly = true;
                }
                for (int i = 0; i < dataView.Rows.Count; i++)
                {
                    dataTable.Rows.Add(dataView.Rows[i].Cells["ID_Recepty"].Value.ToString(),
                        dataView.Rows[i].Cells["ID_Wizyty"].Value.ToString(),
                        dataView.Rows[i].Cells["Pacjent"].Value.ToString(),
                        dataView.Rows[i].Cells["Lekarz"].Value.ToString(),
                        dataView.Rows[i].Cells["Data_Wizyty"].Value.ToString());
                }
                columnsCombo.SelectedIndex = 0;
                filterValue.Text = "";
            }
        }

        private void FillGridWithDiagnosisData()
        {
            if (patientsButton.BackColor == Color.Silver)
            {
                dataView.Columns.Clear();
                dataView.DataSource = PatientsData.GetDiagnosisTable();
                int count = dataView.ColumnCount;
                columnsCombo.Items.Clear();
                dataTable = new DataTable();
                for (int i = 0; i < dataView.ColumnCount; i++)
                {
                    columnsCombo.Items.Add(dataView.Columns[i].Name);
                    dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                    dataTable.Columns[i].ReadOnly = true;
                    dataView.Columns[i].ReadOnly = true;
                }
                for (int i = 0; i < dataView.Rows.Count; i++)
                {
                    dataTable.Rows.Add(dataView.Rows[i].Cells["ID_Diagnozy"].Value.ToString(),
                        dataView.Rows[i].Cells["ID_Wizyty"].Value.ToString(),
                        dataView.Rows[i].Cells["Pacjent"].Value.ToString(),
                        dataView.Rows[i].Cells["Lekarz"].Value.ToString(),
                        dataView.Rows[i].Cells["Nazwa_Diagnozy"].Value.ToString(),
                        dataView.Rows[i].Cells["Data_Wizyty"].Value.ToString());
                }
                columnsCombo.SelectedIndex = 0;
                filterValue.Text = "";
            }
            if (doctorsButton.BackColor == Color.Silver)
            {
                dataView.Columns.Clear();
                dataView.DataSource = DoctorsData.GetDiagnosisTable();
                int count = dataView.ColumnCount;
                columnsCombo.Items.Clear();
                dataTable = new DataTable();
                for (int i = 0; i < dataView.ColumnCount; i++)
                {
                    columnsCombo.Items.Add(dataView.Columns[i].Name);
                    dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));
                    dataTable.Columns[i].ReadOnly = true;
                    dataView.Columns[i].ReadOnly = true;
                }
                for (int i = 0; i < dataView.Rows.Count; i++)
                {
                    dataTable.Rows.Add(dataView.Rows[i].Cells["ID_Wizyty"].Value.ToString(),
                        dataView.Rows[i].Cells["Pacjent"].Value.ToString(),
                        dataView.Rows[i].Cells["Lekarz"].Value.ToString(),
                        dataView.Rows[i].Cells["Nazwa_Diagnozy"].Value.ToString(),
                        dataView.Rows[i].Cells["Data_Wizyty"].Value.ToString());
                }
                columnsCombo.SelectedIndex = 0;
                filterValue.Text = "";
            }
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void SetVisible(bool visible)
        {
            foreach (Control control in this.Controls)
            {
                control.Visible = visible;
            }
        }

        private void dodajToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string text = this.Text;
            ManageUsers manageUsers = new ManageUsers(username,user.Text);
            this.Hide();
            manageUsers.ShowDialog();
            this.Show();
        }

        private void doctorsButton_Click(object sender, EventArgs e)
        {
            patientsButton.BackColor = Color.Transparent;
            doctorsButton.BackColor = Color.Silver;
            addNew.Enabled = true;
            editPerson.Enabled = true;
            delete.Enabled = true;
            FillGridWithDoctorsData();
        }

        private void patientsButton_Click(object sender, EventArgs e)
        {
            doctorsButton.BackColor = Color.Transparent;
            patientsButton.BackColor = Color.Silver;
            addNew.Enabled = true;
            editPerson.Enabled = true;
            delete.Enabled = true;
            FillGridWithPatientsData();
        }

        private void filterButton_Click(object sender, EventArgs e)
        {
            dataTable.DefaultView.RowFilter = string.Format("{0} LIKE '%{1}%'", columnsCombo.Text, filterValue.Text);
            dataView.DataSource = dataTable;         
        }

        private void addNew_Click(object sender, EventArgs e)
        {
            if (patientsButton.BackColor == Color.Silver)
            {
                AddPatient addPatient = new AddPatient();
                if (addPatient.ShowDialog() == DialogResult.OK)
                {
                    addPatient.AddToDatabase();
                    FillGridWithPatientsData();
                }
            }
            if (doctorsButton.BackColor == Color.Silver)
            {
                AddDoctor addDoctor = new AddDoctor();
                if (addDoctor.ShowDialog() == DialogResult.OK)
                {
                    addDoctor.AddToDatabase();
                    FillGridWithDoctorsData();
                }
            }
        }

        private void editPerson_Click(object sender, EventArgs e)
        {
            if (patientsButton.BackColor == Color.Silver && dataView.SelectedRows.Count > 0)
            {
                string selectedID = "",selectedForename = "", selectedSurname = "", selectedCity = "", selectedStreet = "",selectedHouseNumber = "",
                 selectedPostalCode = "",selectedPesel = "";
                selectedID = dataView.SelectedRows[0].Cells["ID"].Value.ToString();
                selectedForename = dataView.SelectedRows[0].Cells["Imię"].Value.ToString();
                selectedSurname = dataView.SelectedRows[0].Cells["Nazwisko"].Value.ToString();
                selectedCity = dataView.SelectedRows[0].Cells["Miasto"].Value.ToString();
                selectedStreet = dataView.SelectedRows[0].Cells["Ulica"].Value.ToString();
                selectedHouseNumber = dataView.SelectedRows[0].Cells["NrDomu"].Value.ToString();
                selectedPostalCode = dataView.SelectedRows[0].Cells["Kod_Pocztowy"].Value.ToString();
                selectedPesel = dataView.SelectedRows[0].Cells["Pesel"].Value.ToString();
                AddPatient addPatient = new AddPatient(selectedID,selectedForename, selectedSurname, selectedCity, selectedStreet,
                    selectedHouseNumber, selectedPostalCode, selectedPesel);
                if (addPatient.ShowDialog() == DialogResult.OK)
                {
                    addPatient.SaveChangesToDatabase();
                    FillGridWithPatientsData();
                }
            }
            if (doctorsButton.BackColor == Color.Silver && dataView.SelectedRows.Count > 0)
            {
                string selectedID = "", selectedForename = "", selectedSurname = "", selectedCity = "", selectedStreet = "", selectedHouseNumber = "",
                 selectedPostalCode = "", selectedPesel = "",selectedType = "";
                selectedID = dataView.SelectedRows[0].Cells["ID"].Value.ToString();
                selectedForename = dataView.SelectedRows[0].Cells["Imię"].Value.ToString();
                selectedSurname = dataView.SelectedRows[0].Cells["Nazwisko"].Value.ToString();
                selectedCity = dataView.SelectedRows[0].Cells["Miasto"].Value.ToString();
                selectedStreet = dataView.SelectedRows[0].Cells["Ulica"].Value.ToString();
                selectedHouseNumber = dataView.SelectedRows[0].Cells["NrDomu"].Value.ToString();
                selectedPostalCode = dataView.SelectedRows[0].Cells["Kod_Pocztowy"].Value.ToString();
                selectedPesel = dataView.SelectedRows[0].Cells["Pesel"].Value.ToString();
                selectedType = dataView.SelectedRows[0].Cells["Specjalizacja"].Value.ToString();
                AddDoctor addDoctor = new AddDoctor(selectedID, selectedForename, selectedSurname, selectedCity, selectedStreet,
                    selectedHouseNumber, selectedPostalCode, selectedPesel,selectedType);
                if (addDoctor.ShowDialog() == DialogResult.OK)
                {
                    addDoctor.SaveChangesToDatabase();
                    FillGridWithDoctorsData();
                }
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            string selectedID = dataView.SelectedRows[0].Cells["ID"].Value.ToString();
            string selectedForename = dataView.SelectedRows[0].Cells["Imię"].Value.ToString();
            string selectedSurname = dataView.SelectedRows[0].Cells["Nazwisko"].Value.ToString();
            if (patientsButton.BackColor == Color.Silver&&dataView.SelectedRows.Count >0)
            {
                if (DialogResult.Yes == MessageBox.Show("Na pewno chcesz usunąć pacjenta " + selectedForename + " " + selectedSurname + "?", "Usuwanie pacjenta", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                {
                    PatientsData.DeletePatient(selectedID);
                    MessageBox.Show("Pacjent " + selectedForename + " " + selectedSurname + " został usunięty.");
                    FillGridWithPatientsData();
                }
            }
            if (doctorsButton.BackColor == Color.Silver && dataView.SelectedRows.Count > 0)
            {
                if (DialogResult.Yes == MessageBox.Show("Na pewno chcesz usunąć doktora " + selectedForename + " " + selectedSurname + "?", "Usuwanie pacjenta", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                {
                    DoctorsData.DeleteDoctor(selectedID);
                    MessageBox.Show("Doktor " + selectedForename + " " + selectedSurname + " został usunięty.");
                    FillGridWithDoctorsData();
                }
            }
        }

        private void visits_Click(object sender, EventArgs e)
        {
            addNew.Enabled = false;
            editPerson.Enabled = false;
            delete.Enabled = false;
            FillGridWithVisitsData();
        }

        private void prescriptions_Click(object sender, EventArgs e)
        {
            addNew.Enabled = false;
            editPerson.Enabled = false;
            delete.Enabled = false;
            FillGridWithPrescriptionsData();
        }

        private void diagnosis_Click(object sender, EventArgs e)
        {
            addNew.Enabled = false;
            editPerson.Enabled = false;
            delete.Enabled = false;
            FillGridWithDiagnosisData();
        }
    }
}
